/*
 * @Description: 
 * @version: 
 * @Company: 
 * @Author: GeYang
 * @Date: 2018-12-10 17:19:02
 * @LastEditors  : Please set LastEditors
 * @LastEditTime : 2019-12-24 13:03:33
 */
/**
 * Created by GeYang on 2018/12/06.
 */
import Axios from 'axios'
import * as utils from './utils'
import {
  Message
} from 'element-ui';
// import Message from 'element-ui'

const $http = Axios.create();

// Before request 拦截器
$http.interceptors.request.use(
  config => {
    config.url = utils.getApiUrl(config.url);
    if (config.method === 'post') {
      const querystring = require('querystring');
      config.data = querystring.stringify(config.data);
      console.log(config.data);
    }
    // let token = utils.getStore('token');
    // if (token) {
    //   config.headers.Authorization = token;
    //   config.headers.Token = token;
    // }
    return config;
  },
  error => {
    return Promise.reject(error);
  }

)

// After request
$http.interceptors.response.use(
  response => {
    console.log(response.data);
    response = response.data;
    // let data = response.data;
    if (response.ret === '40001' || response.ret === '40018') {
      // response.msg !== '' && alert(response.msg);
      return response;
    } else if (response.ret === '40003') {
      // TODO: 登录超时
      $router.replace();
      return new Promise(() => {});
    } else if (response.ret !== '40001' && response.ret !== '40003' && response.ret !== '40018') {
      Message.error(response.msg);
    } else {
      // alert(response.msg + '未知错误', {
      //   confirmButtonTextL: '确定',
      //   callback: action => {
      //     this.message({
      //       type: 'info',
      //       message: response.ret
      //     })
      //   }
      // })
      Message.error(response.msg);
    }
  }
)
export default $http;
